package com.bw.permission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.permission.common.AjaxResult;
import com.bw.permission.common.BizException;
import com.bw.permission.entity.Area;
import com.bw.permission.service.IAreaService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2021-08-18
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private IAreaService iAreaService;

    /**
     * 根据Pid查询城市列表
     * @param pid
     * @return
     */
//    @RequestMapping(value = "getList",method = RequestMethod.GET)
    @GetMapping("getListByPid")
    public AjaxResult getList(@RequestParam(value = "pid",defaultValue = "1") Integer pid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", pid);
        return AjaxResult.ok(iAreaService.list(queryWrapper));
    }

    /**
     * @param pid
     * @return
     */
    @GetMapping("getTreeData")
    public AjaxResult getTreeData(@RequestParam(value = "pid", defaultValue = "1") Integer pid) {
        //查询省list
        List<Area> shengList = iAreaService.getListByPid(pid);
        //查询市
        //list、set、map 否forEach
        shengList.forEach(sheng->{
            //根据省Id，查询市
            List<Area> shiList = iAreaService.getListByPid(sheng.getId());
            sheng.setSub(shiList);
            //根据市Id查询县
            shiList.forEach(shi->{
                List<Area> xianList = iAreaService.getListByPid(shi.getId());
                shi.setSub(xianList);
            });
        });
        return AjaxResult.ok(shengList);
    }
}
